package com.example.demo.controller;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.example.demo.enity.Msg;
import com.example.demo.enity.RecruitmentInfo;

import com.example.demo.enity.UserInfo;
import com.example.demo.enity.UserRole;
import com.example.demo.service.BaseService;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 登录用户基本操作，控制层
 * @author acer
 *
 */
@Controller
public class BaseController {

	@Autowired
	UserService userService;
	@Autowired
	BaseService baseService;
	/**
	 * 1.用户安全配置文件配置自定义登录页面时，需要一个返回自定义页面的控制器
	 * @return
	 */
	  @RequestMapping("/login") 
	  public String Login() 
	  { 
		  return "login"; 
		  
	  }
	  /**
	   * 1.返回主界面
	   * @return
	   */
	  @RequestMapping("/base/mainpage") 
	  public String mainpage() 
	  { 
		  return "mainpage"; 
		  
	  }
	  /**
	   * 1.返回注册界面
	   * @return
	   */
	  @RequestMapping("/base/registerpage") 
	  public String registerpage() 
	  { 
		  return "registerpage"; 
		  
	  }
	 
	  /**
	   * 1.模糊查询搜索工作
	   * @param pages
	   * @param zpname
	   * @return
	   */
	  @RequestMapping(value = "/base/search",method = RequestMethod.POST)
	  @ResponseBody
	  public Msg search(@RequestParam(value = "pages",defaultValue = "1") Integer pages,@RequestParam String zpname) 
	  { 
		if(baseService.selectRecruitmentInfoByLikeZpname(zpname)!=null) {
			
			  PageHelper.startPage(pages,5);
			  PageHelper.orderBy("zpid desc");
			  List<RecruitmentInfo>rems=baseService.selectRecruitmentInfoByLikeZpname(zpname);
			  PageInfo<RecruitmentInfo> remInfo=new PageInfo<RecruitmentInfo>(rems,5);
			  return Msg.success().add("reminfos",remInfo);
		}else {
			return Msg.fail();
		}
	  }
	  
	  /**
	   * 根据招聘信息单个项查询详细招聘信息
	   * @param zpid
	   * @return
	   */
	  @RequestMapping(value="/base/selectRecByPrimaryKey",method = RequestMethod.POST)
	  @ResponseBody
	  public Msg selectRecByPrimaryKey(Integer zpid) 
	  { 
		
		  if(baseService.selectRecByPrimaryKey(zpid)!=null) {
			  return Msg.success().add("OneRec", baseService.selectRecByPrimaryKey(zpid));
		  }else {
			return Msg.fail();	
			}
	 
	  }
	  
	  /**
	   * 新用户注册
	   * @param userInfo
	   * @return
	   */
	  @RequestMapping(value = "/base/inertNewUserSelective",method = RequestMethod.POST) 
	  @ResponseBody
	  public Msg inertNewUserSelective(UserInfo userinfo) {

		 
		  if(userService.loadUserInfoByUtel(userinfo.getUtel())==null) {
			  userinfo.setFcount(0);
			  userinfo.setUenable(true);
			  userinfo.setUlocked(false);
			  userinfo.setTime(new Date());
			  baseService.insertUesrinfoSelective(userinfo);
			  UserRole userRole=new UserRole();
			  userRole.setRid(1);
			  userRole.setUid(userService.loadUserInfoByUtel(userinfo.getUtel()).getUid());
			  baseService.insertUserRoleSelective(userRole);
			  return Msg.success();	
		  }else {
			  return Msg.fail();
		  }
		  
		  
	  }
	 
	 
	  
	 
	 
	  
	 
	  
	 
	/**
	 * 根据招聘id查询招聘信息
	 * @param zpid
	 * @return
	 */
	  @RequestMapping(value="/base/deleteRecruitmentByZpid",method =RequestMethod.POST)
	  @ResponseBody
	  public Msg deleteRecruitmentByZpid(Integer zpid) {
		  return Msg.success().add("deleteRecruitmentByZpid", userService.deleteRecruitmentByZpid(zpid));
	  }
	  /**
	   * 根据职位性质zwid查询招聘信息
	   * @param zwid
	   * @param pages
	   * @return
	   */
	  @RequestMapping(value="/base/selectRecruitmentInfoByZwId",method =RequestMethod.POST) 
	  @ResponseBody
	  public Msg selectRecruitmentInfoByZwId(@RequestParam Integer zwid,@RequestParam(value = "pages",defaultValue = "1") int pages) 
	  { 
		  PageHelper.startPage(pages, pages);
		  PageHelper.orderBy("zwid asc");
		  List<RecruitmentInfo> remInfo=userService.selectRecruitmentInfoByZwId(zwid);
		  PageInfo<RecruitmentInfo> remInfos=new PageInfo<>(remInfo);
		  
		  return  Msg.success().add("RemInfo",remInfos);
	  }
	 
	  
	  
	 
	
	
}
